import java.util.Arrays;
import java.util.Iterator;

public class MyArraylist_2<T>implements mlist_2  {
    Object[] elem;
    int size;
public MyArraylist_2(){
    elem=new Object[10];
}
    @Override
    public void add(Object val) {
    if(IsFull()){
     exsize();
    }
    elem[size++]=val;
    }

    @Override
    public void add(int index, Object val) {
        if (index < 0 || index > elem.length || index - size > 1) {
            throw new RuntimeException("index error");
        }
        if (IsFull()){
            exsize();
        }
        elem[index]=val;
        size++;
    }

    @Override
    public boolean IsFull() {
        return this.size==elem.length;
    }

    @Override
    public void exsize() {
    elem= Arrays.copyOf(elem,2*elem.length);
    }

    @Override
    public boolean contains(Object val) {
    if(getsize()==0){
        throw new RuntimeException("array empty");
    }
    for(int i=0;i<getsize();i++){
        if(elem[i]==val){//Object compare
            return true;
        }
    }
        return false;
    }

    @Override
    public int Indexof(Object val) {
        if(getsize()==0){
            throw new RuntimeException("array empty");
        }
        for(int i=0;i<getsize();i++){
            if(elem[i]==val){//Object compare
                return i;
            }
        }
        return -1;
    }

    @Override
    public Object get(int index) {
    if(getsize()==0){
        throw new RuntimeException("array empty");
    }
    if(index<0||index>elem.length){
        throw new RuntimeException("index error");
    }
        return elem[index];
    }

    @Override
    public void set(int index, Object val) {
        if(getsize()==0){
            throw new RuntimeException("array empty");
        }
        if(index<0||index>elem.length){
            throw new RuntimeException("index error");
        }
        elem[index]=val;
    }

    @Override
    public int getsize() {
        return size;
    }

    @Override
    public void remove(int index) {
        if(getsize()==0){
            throw new RuntimeException("array empty");
        }
        if(index<0||index>elem.length){
            throw new RuntimeException("index error");
        }
        for(int i=index;i<elem.length-1;i++){
            elem[i]=elem[i+1];
        }
        size--;
    }

    @Override
    public void clear() {
    size=0;
    }


}


class Cat{
    private String name;
    private int age;
    private double preice;

    public Cat(String name, int age, double preice) {
        this.name = name;
        this.age = age;
        this.preice = preice;
    }
}

class Dog{
    private String name;
    private int age;
    private double preice;

    public Dog(String name, int age, double preice) {
        this.name = name;
        this.age = age;
        this.preice = preice;
    }
}

class  Test_2 {

    public static void main(String[] args) {
        MyArraylist_2<Object> list = new MyArraylist_2<Object>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.remove(3);
        list.add("hello world");
        list.add(new Cat("小黄", 3, 500));
        list.add(new Cat("Tom", 5, 600));
        list.add(new Dog("肥波", 6, 300));

    }
}